﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace ExceptionHandling
{
    [TestClass]
    public class FilterableHandlerFixture
    {
        private void Invoke(Func<MyException> func)
        {
            try
            {
                throw func();
            }
            catch (Exception ex)
            {
                if (ExceptionPolicy.HandleException(ex, "Policy"))
                {
                    throw;
                }
            }
        }

        [ExpectedException(typeof(FooException))]
        [TestMethod]
        public void TestMethod1()
        {
            this.Invoke(() => new MyException { Number = 1 });
        }

        [ExpectedException(typeof(BarException))]
        [TestMethod]
        public void TestMethod2()
        {
            this.Invoke(() => new MyException { Number = 2 });
        }

        [ExpectedException(typeof(BazException))]
        [TestMethod]
        public void TestMethod3()
        {
            this.Invoke(() => new MyException { Number = 3 });
        }
    }

    [Serializable]
    public class MyException : Exception
    {
        public int Number { get; set; }
    }

    [Serializable]
    public class FooException : Exception
    {
        public FooException() { }
        public FooException(string message) : base(message) { }
        public FooException(string message, Exception inner) : base(message, inner) { }
        protected FooException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    [Serializable]
    public class BarException : Exception
    {
        public BarException() { }
        public BarException(string message) : base(message) { }
        public BarException(string message, Exception inner) : base(message, inner) { }
        protected BarException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    [Serializable]
    public class BazException : Exception
    {
        public BazException() { }
        public BazException(string message) : base(message) { }
        public BazException(string message, Exception inner) : base(message, inner) { }
        protected BazException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
}
